import { _decorator, Component, EventTouch, instantiate, Layers, Node, Prefab, Sprite, tween, CCInteger, v3, UITransform } from 'cc';
import { ImgMgr } from './ImgMgr';
const { ccclass, property } = _decorator;

@ccclass('GameRoot')
export class GameRoot extends Component {
    @property(Node) pointsLayout: Node;

    // 牌预制体
    @property(Prefab)
    cardPrefab: Prefab = null
    
    @property(CCInteger) // 声明属性的 cc 类型为整数
    card_val_node: number = 0;

    /**初始化牌堆 */
    cardsData: number[] = [101, 101, 102, 102, 103, 103, 104, 104, 105, 105];//这个可以是后台数据

    

    /**当前点击卡牌 */
    curOpenCard = {
        node: null,
        data: -1,
    }

    start() {
        this.initPoker()
    }

    private async initPoker(){
        this.orderAllCards();//洗牌
        this.createAllCards();
        await this.cardsFlipToFrontOrBack();//正面翻转反面
        this.initTouchEvent()
    }

    /**触摸事件 */
    private async onTouchEnd(event: EventTouch){

        tween(event.target)
            .to(0.2, { scale: v3(0, 1, 1) })
            .call( async () => {
                await event.target.getComponent(ImgMgr).flipToFrontOrBack(event.target, false);//显示正面
                
            })
            .to(0.2, { scale: v3(1, 1, 1) })
            .start();

    }

    private initTouchEvent() {
        this.pointsLayout.children.forEach( (node, index) => {
            // console.log("index: ", index);
            node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this)
        })
    }

    private offTouchEvent() {
        this.pointsLayout.children.forEach( (node, index) => {
            node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this)

        })
    }

    protected onDestroy(): void {
        // this.offTouchEvent();//释放
    }

    /**洗牌 */
    orderAllCards() {
        //洗多少次
        for (let index = 0; index < 3; index++) {
            this.cardsData.sort(() => 0.5 - Math.random());
        }
    }

    /**翻牌动画 */
    cardsFlipToFrontOrBack() {
        return new Promise<void>(res => {
            this.pointsLayout.children.forEach( (node, index) => {
                let cardCtrl = node.getComponent(ImgMgr)
                tween(node)
                .delay(2)
                .to(0.2, { scale: v3(0, 1, 1) })
                .call( async () => {
                    await cardCtrl.flipToFrontOrBack(node);//显示背面
                })
                .to(0.2, { scale: v3(1, 1, 1) })
                .start();
                this.scheduleOnce(() => {
                    res();
                },1.2)
            })

        });
        
    }

    /**创建牌显示 */
    createAllCards() {
        this.pointsLayout.removeAllChildren();//移除所有子节点
        // console.log("createAllCards: ", this.cardsData);
        this.cardsData.forEach( val => {
            this.createOneCard(val)
        })
        // console.log("pointsLayout: ", this.pointsLayout);
    }

    createOneCard(val: number) {
        if (!(val > 0)) { return; }
        let cardNode = instantiate(this.cardPrefab);// 创建牌预制体
        let cardCtrl = cardNode.getComponent(ImgMgr);
        this.pointsLayout.addChild(cardNode);

        if(cardNode.layer != Layers.Enum.UI_2D) {
            cardNode.layer = Layers.Enum.UI_2D;//这个问题特别注意
        }
        // this.pointsLayout.getComponent(UITransform).setContentSize(116,159);
        
        cardCtrl.init(val);    // 初始化牌预制体数据
    }

}


